package com.zhny.sensor.service;

import cn.ucloud.common.pojo.Account;
import cn.ucloud.common.pojo.BaseResponseResult;
import cn.ucloud.usms.client.DefaultUSMSClient;
import cn.ucloud.usms.client.USMSClient;
import cn.ucloud.usms.model.SendUSMSMessageParam;
import cn.ucloud.usms.model.SendUSMSMessageResult;
import cn.ucloud.usms.pojo.USMSConfig;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zhny.sensor.App;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UsmsService {

    private USMSClient client;

    private SendUSMSMessageParam param;

    private static final Logger logger = LoggerFactory.getLogger(App.class.getName());

    public static void jsonComparator(BaseResponseResult result) {
        try {
            Gson gson = new GsonBuilder().serializeNulls().create();
            logger.info("result 2 json:{}", gson.toJson(result));
            JSONAssert.assertEquals(result.getResponseContent(), gson.toJson(result), JSONCompareMode.LENIENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 发送短信服务
     * @param phone  维修人员手机号
     * @param stalls_id  档口id
     * @param number  设备号
     * @param msg   故障信息
     * @throws Exception
     */
    public void sendMessage(String phone,String stalls_id,String number,String msg) throws Exception {
        client = new DefaultUSMSClient(new USMSConfig(
                new Account("e4ec47efad0708bd830121deb1e137dbb1a17af0",
                        "Tx1SRcYLRQ2KEGeYIbhBeWoXVBYmqVKgZR/NDISY9qICQ9h34acDrw==")));
        List<String> phoneNumbers = new ArrayList<>();
        //手机号
        phoneNumbers.add(phone);
        //短信模板id
        String templateId = "UTN201112753530";
        param = new SendUSMSMessageParam(phoneNumbers, templateId);
        //签名内容
        param.setSigContent("仲恺农业工程学院");
        //项目id
        param.setProjectId("org-jdrgnd");
        //短信参数设置
        List<String> templateParams = new ArrayList<>();
        templateParams.add(stalls_id);
        templateParams.add(number);
        templateParams.add(msg);
        param.setTemplateParams(templateParams);

        try {
            SendUSMSMessageResult result = client.sendUSMSMessage(param);
            jsonComparator(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
